package org.ccay.security.authc;

import org.apache.shiro.authc.HostAuthenticationToken;
import org.apache.shiro.authc.RememberMeAuthenticationToken;
import org.ccay.security.entity.User;

public class UserInfoPasswordToken implements HostAuthenticationToken, RememberMeAuthenticationToken {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * The userInfo
     */
    private User user;

    /**
     * The password, in char[] format
     */
    private char[] password;

    /**
     * Whether or not 'rememberMe' should be enabled for the corresponding login attempt;
     * default is <code>false</code>
     */
    private boolean rememberMe = false;

    /**
     * The location from where the login attempt occurs, or <code>null</code> if not known or explicitly
     * omitted.
     */
    private String host;

    /*--------------------------------------------
    |         C O N S T R U C T O R S           |
    ============================================*/

    /**
     * JavaBeans compatible no-arg constructor.
     */
    public UserInfoPasswordToken() {
    }

    public UserInfoPasswordToken(final String username, final char[] password) {
        this(username, password, false, null);
    }

    public UserInfoPasswordToken(final String username, final String password) {
        this(username, password != null ? password.toCharArray() : null, false, null);
    }

    public UserInfoPasswordToken(final String username, final char[] password, final String host) {
        this(username, password, false, host);
    }

    public UserInfoPasswordToken(final String username, final String password, final String host) {
        this(username, password != null ? password.toCharArray() : null, false, host);
    }

    public UserInfoPasswordToken(final String username, final char[] password, final boolean rememberMe) {
        this(username, password, rememberMe, null);
    }

    public UserInfoPasswordToken(final String username, final String password, final boolean rememberMe) {
        this(username, password != null ? password.toCharArray() : null, rememberMe, null);
    }

    public UserInfoPasswordToken(final String username, final char[] password, final boolean rememberMe, final String host) {
        this(new User(username),password,rememberMe,host);
    }
    
    public UserInfoPasswordToken(final User userInfo, final String password,final boolean rememberMe, final String host) {
    	this(userInfo,password != null ? password.toCharArray() : null,rememberMe,host);
    }
    
    public UserInfoPasswordToken(final User userInfo, final char[] password,final boolean rememberMe, final String host) {
    	this.user = userInfo;
    	this.password = password;
    	this.rememberMe = rememberMe;
    	this.host = host;
    }

    public UserInfoPasswordToken(final String username, final String password,
                                 final boolean rememberMe, final String host) {
        this(username, password != null ? password.toCharArray() : null, rememberMe, host);
    }


    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public char[] getPassword() {
        return password;
    }

    public void setPassword(char[] password) {
        this.password = password;
    }

    public Object getPrincipal() {
        return getUser();
    }

    public Object getCredentials() {
        return getPassword();
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
    public boolean isRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    public void clear() {
        this.user = null;
        this.host = null;
        this.rememberMe = false;

        if (this.password != null) {
            for (int i = 0; i < password.length; i++) {
                this.password[i] = 0x00;
            }
            this.password = null;
        }

    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(" - ");
        sb.append(user);
        sb.append(", rememberMe=").append(rememberMe);
        if (host != null) {
            sb.append(" (").append(host).append(")");
        }
        return sb.toString();
    }

}
